package com.wei.web.middleware.httpfilter.auth.security;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.wei.web.middleware.httpfilter.filter.init.FilterInitialize;
import com.wei.web.middleware.httpfilter.registrar.model.FilterConfigDTO;
import com.wei.web.middleware.httpfilter.utils.FileUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

import java.io.IOException;
import java.util.*;

public class AuthenticationFilter extends AbstractAuthenticationProcessingFilter implements FilterInitialize {

    public static final String RESOURCE_LOCATION = "auth/security_config.json";
    private Map<String, String> pathToAuthMethodMap = new HashMap<>();

    public AuthenticationFilter(AuthenticationManager authenticationManager) {
        super("/**");
        setAuthenticationManager(authenticationManager);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        System.out.println("auth");
        Authentication authentication = new Authentication() {
            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                return Arrays.asList(new GrantedAuthority() {
                    @Override
                    public String getAuthority() {
                        return "aaa";
                    }
                });
            }

            @Override
            public Object getCredentials() {
                return "cred";
            }

            @Override
            public Object getDetails() {
                return "detail";
            }

            @Override
            public Object getPrincipal() {
                return "Principal";
            }

            @Override
            public boolean isAuthenticated() {
                return true;
            }

            @Override
            public void setAuthenticated(boolean b) throws IllegalArgumentException {

            }

            @Override
            public String getName() {
                return "name";
            }
        };
        if (getAuthenticationManager() == null) {
            return authentication;
        }
        return getAuthenticationManager().authenticate(authentication);
    }

    @Override
    public void init(FilterConfigDTO filterConfigDTO) {
        String filePath = RESOURCE_LOCATION;
        ClassLoader classLoader = AuthenticationFilter.class.getClassLoader();
        String configStr = null;
        try {
            configStr = FileUtil.getContent(filePath, classLoader);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        List<AuthConfigDTO> authConfigDTOS = JSONObject.parseObject(configStr, new TypeReference<>() {});

        authConfigDTOS.stream().forEach(t->{
            for (String path :t.getPath()){
                pathToAuthMethodMap.put(path, t.getAuthMethod());
            }
         });

    }



    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class AuthConfigDTO{
        private List<String> path;
        private String authMethod;
    }
}
